package tech.wetech.admin3.sys.service;

import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.wetech.admin3.sys.model.AooUser;
import tech.wetech.admin3.sys.repository.AooUserRepository;

import java.util.List;

@Service
public class AooUserService {
  private AooUserRepository aooUserRepository;
  private PasswordEncoder passwordEncoder;
  private AooPhoneCodeService phoneCodeService;

  public AooUserService(
    AooUserRepository aooUserRepository,
    PasswordEncoder passwordEncoder,
    AooPhoneCodeService phoneCodeService
  ){
    this.aooUserRepository=aooUserRepository;
    this.passwordEncoder = passwordEncoder;
    this.phoneCodeService = phoneCodeService;
  }

  // 注册
  public AooUser register(AooUser aooUser){
    aooUser.setPassword(passwordEncoder.encode(aooUser.getPassword()));
    return aooUserRepository.save(aooUser);
  }

  public boolean checkPassword(String email, String password){
    AooUser user = aooUserRepository.findByEmail(email);
    return user != null && passwordEncoder.matches(password, user.getPassword());
  }

//  @Transactional
//  public boolean resetPwByEmail(String email, String newPassword){
//    AooUser aooUser = aooUserRepository.findByEmail(email);
//    if(aooUser != null){
//      // 用户存在
//      aooUser.setPassword(passwordEncoder.encode(newPassword));
//      aooUserRepository.save(aooUser);
//      return true;
//    } else {
//      // 查找不到用户
//      return false;
//    }
//  }

  @Transactional
  public boolean updateRole(String email, String newRole){
    AooUser aooUser = aooUserRepository.findByEmail(email);
    if(aooUser != null){
      // 用户存在
      aooUser.setRole(newRole);
      aooUserRepository.save(aooUser);
      return true;
    } else {
      // 查找不到用户
      return false;
    }
  }

  public List<AooUser> getAllUser(){
    return aooUserRepository.findAll();
  }

  public AooUser findByEmail(String email) {
    return aooUserRepository.findByEmail(email);
  }
  public AooUser findByPhone(String phone) {
    return aooUserRepository.findByPhone(phone);
  }

  public void deleteUser(String email){
    aooUserRepository.deleteByEmail(email);
  }

  public void update(String email, String key, String value) {
    AooUser user = aooUserRepository.findByEmail(email);
    if (user == null) {
      throw new UserNotFoundException("用户不存在");
    }
    switch (key) {
      case "username":
        user.setUsername(value);
        break;
      case "avatarUrl":
        user.setAvatarUrl(value);
        break;
      case "password":
        user.setPassword(passwordEncoder.encode(value));
        break;
      default:
        throw new IllegalArgumentException("不支持的key: " + key);
    }
    aooUserRepository.save(user);
  }

  public AooUser admin_update(String email, String key, Integer value, String vericode) {
    boolean flag = phoneCodeService.admin_VerifyCode(vericode);
    if(!flag){
      throw new IllegalArgumentException("admin 验证码错误或已过期");
    }

    AooUser user = aooUserRepository.findByEmail(email);
    if (user == null) {
      throw new UserNotFoundException("用户不存在");
    }

    switch (key) {
      case "authType":
        user.setAuthType(value);
        user.setStatus(2);
        break;
//      case "avatarUrl":
//        user.setAvatarUrl(value);
//        break;
//      case "password":
//        user.setPassword(passwordEncoder.encode(value));
//        break;
      default:
        throw new IllegalArgumentException("不支持的key: " + key);
    }
    return aooUserRepository.save(user);
  }


  public class UserNotFoundException extends RuntimeException {
    public UserNotFoundException(String message) {
      super(message);
    }
    // 可以添加更多的构造函数、方法等
  }

}


